package com.nsxy.common.service;

import java.io.Serializable;
import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;

import com.nsxy.common.dao.BaseRepository;

/**
 * @author 潜行的青衣
 * @see
 */
public class BaseService<T, ID extends Serializable, R extends BaseRepository<T, ID>> implements IBaseService<T, ID, R> {
	
	protected R repository;

	public BaseService(R repository) {
		this.repository = repository;
	}
	
	/* (non-Javadoc)
	 * @see com.nsxy.common.service.IBaseService#findAll()
	 */
	@Override
	public List<T> findAll() {
		return this.getRepository().findAll();
	}

	/* (non-Javadoc)
	 * @see com.nsxy.common.service.IBaseService#findAll(org.springframework.data.domain.Sort)
	 */
	@Override
	public List<T> findAll(Sort sort) {
		return this.getRepository().findAll(sort);
	}

	/* (non-Javadoc)
	 * @see com.nsxy.common.service.IBaseService#findAll(java.lang.Iterable)
	 */
	@Override
	public List<T> findAll(Iterable<ID> ids) {
		return this.getRepository().findAll(ids);
	}

	/* (non-Javadoc)
	 * @see com.nsxy.common.service.IBaseService#save(java.lang.Iterable)
	 */
	@Override
	public <S extends T> List<S> save(Iterable<S> entities) {
		return this.getRepository().save(entities);
	}

	/* (non-Javadoc)
	 * @see com.nsxy.common.service.IBaseService#deleteInBatch(java.lang.Iterable)
	 */
	@Override
	public void deleteInBatch(Iterable<T> entities) {
		this.getRepository().deleteInBatch(entities);
	}

	/* (non-Javadoc)
	 * @see com.nsxy.common.service.IBaseService#deleteAllInBatch()
	 */
	@Override
	public void deleteAllInBatch() {
		this.getRepository().deleteAllInBatch();
	}

	/* (non-Javadoc)
	 * @see com.nsxy.common.service.IBaseService#findOne(java.io.Serializable)
	 */
	@Override
	public T findOne(ID id) {
		return this.getRepository().findOne(id);
	}

	/* (non-Javadoc)
	 * @see com.nsxy.common.service.IBaseService#exists(java.io.Serializable)
	 */
	@Override
	public boolean exists(ID id) {
		return this.getRepository().exists(id);
	}

	/* (non-Javadoc)
	 * @see com.nsxy.common.service.IBaseService#findOne(org.springframework.data.jpa.domain.Specification)
	 */
	@Override
	public T findOne(Specification<T> spec) {
		return this.getRepository().findOne(spec);
	}

	/* (non-Javadoc)
	 * @see com.nsxy.common.service.IBaseService#findAll(org.springframework.data.jpa.domain.Specification)
	 */
	@Override
	public List<T> findAll(Specification<T> spec) {
		return this.getRepository().findAll(spec);
	}

	/* (non-Javadoc)
	 * @see com.nsxy.common.service.IBaseService#findAll(org.springframework.data.jpa.domain.Specification, org.springframework.data.domain.Pageable)
	 */
	@Override
	public Page<T> findAll(Specification<T> spec, Pageable pageable) {
		return this.getRepository().findAll(spec, pageable);
	}

	/* (non-Javadoc)
	 * @see com.nsxy.common.service.IBaseService#findAll(org.springframework.data.jpa.domain.Specification, org.springframework.data.domain.Sort)
	 */
	@Override
	public List<T> findAll(Specification<T> spec, Sort sort) {
		return this.getRepository().findAll(spec, sort);
	}

	/* (non-Javadoc)
	 * @see com.nsxy.common.service.IBaseService#save(java.lang.Object)
	 */
	@Override
	public <S extends T> S save(S entity) {
		return this.getRepository().save(entity);
	}

	/* (non-Javadoc)
	 * @see com.nsxy.common.service.IBaseService#delete(java.io.Serializable)
	 */
	@Override
	public void delete(ID id) {
		this.getRepository().delete(id);
	}

	/* (non-Javadoc)
	 * @see com.nsxy.common.service.IBaseService#delete(java.lang.Object)
	 */
	@Override
	public void delete(T entity) {
		this.getRepository().delete(entity);
	}

	/* (non-Javadoc)
	 * @see com.nsxy.common.service.IBaseService#delete(java.lang.Iterable)
	 */
	@Override
	public void delete(Iterable<? extends T> entities) {
		this.getRepository().delete(entities);
	}

	/* (non-Javadoc)
	 * @see com.nsxy.common.service.IBaseService#deleteAll()
	 */
	@Override
	public void deleteAll() {
		this.getRepository().deleteAll();
	}

	/* (non-Javadoc)
	 * @see com.nsxy.common.service.IBaseService#count(org.springframework.data.jpa.domain.Specification)
	 */
	@Override
	public long count(Specification<T> spec) {
		return this.getRepository().count(spec);
	}

	/* (non-Javadoc)
	 * @see com.nsxy.common.service.IBaseService#count()
	 */
	@Override
	public long count() {
		return this.getRepository().count();
	}
	
	public R getRepository() {
		return repository;
	}

}
